package com.csjbot.blackgaga.feature.settings;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.csjbot.blackgaga.R;
import com.csjbot.blackgaga.base.BaseFullScreenActivity;
import com.csjbot.blackgaga.base.BasePresenter;
import com.csjbot.blackgaga.feature.settings.settings_list.SettingsListActivity;
import com.csjbot.blackgaga.global.Constants;
import com.csjbot.blackgaga.util.CheckOutUtil;
import com.csjbot.blackgaga.util.SharePreferenceTools;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by jingwc on 2017/10/20.
 */

public class SettingsActivity extends BaseFullScreenActivity implements SettingsContract.View {

    SettingsContract.Presenter mPresenter;
    @BindView(R.id.et_password)
    EditText mEtPassword;
    private String activityName;
    private boolean guideAll;

    @Override
    protected BasePresenter getPresenter() {
        return mPresenter;
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_settings;
    }

    @Override
    public boolean isOpenTitle() {
        return true;
    }

    @Override
    public void afterViewCreated(Bundle savedInstanceState) {
        super.afterViewCreated(savedInstanceState);
        getTitleView().setSettingsVisibility(View.GONE);
        mEtPassword.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)});//最多只能输入10个字符
        CheckOutUtil.getInstance().verifyUseName(mEtPassword, this);

        activityName = getIntent().getStringExtra(Constants.JUMP_ACTIVITY_NAME);
        guideAll = getIntent().getBooleanExtra(Constants.GUIDE_ALL, false);
        if (!TextUtils.isEmpty(activityName)) {
            speak(R.string.input_pwd_first, true);
        }
    }

    @Override
    public void init() {
        super.init();
        mPresenter = new SettingsPresenter();
        mPresenter.initView(this);

        getTitleView().setSettingsPageBackVisibility(View.VISIBLE);
    }

    @OnClick(R.id.bt_login)
    public void login() {
        SharePreferenceTools sharePreferenceTools = new SharePreferenceTools(this);
        if (sharePreferenceTools.getString("pwd_word") != null) {
            if (sharePreferenceTools.getString("pwd_word").equals(mEtPassword.getText().toString().trim())) {
                if (TextUtils.isEmpty(activityName)) {
                    jumpActivity(SettingsListActivity.class);
                } else {
                    try {
                        Class clz = Class.forName(activityName);
                        Intent intent = new Intent(SettingsActivity.this, clz);
                        intent.putExtra(Constants.GUIDE_ALL, guideAll);
                        startActivity(intent);
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                }
                this.finish();
            } else {
                Toast.makeText(this, getString(R.string.passWord_error), 10).show();
                mEtPassword.setText("");
            }
        } else {
            if (mEtPassword.getText().toString().trim().equals("csjbot")) {
                if (TextUtils.isEmpty(activityName)) {
                    jumpActivity(SettingsListActivity.class);
                } else {
                    try {
                        Class clz = Class.forName(activityName);
                        Intent intent = new Intent(SettingsActivity.this, clz);
                        intent.putExtra(Constants.GUIDE_ALL, guideAll);
                        startActivity(intent);
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                }

                this.finish();
            } else Toast.makeText(this, getString(R.string.passWord_error), 10).show();
        }

    }


}
